package com.yonyou.jrmob.contacts.schedule.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.yonyou.jrmob.contacts.R;
import com.yonyou.jrmob.contacts.schedule.adapter.BoardroomAdapter;
import com.yonyou.jrmob.contacts.schedule.vo.MeetRoomVO;
import com.yonyou.uap.um.service.ServiceCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import jrmob.yonyou.com.commonlib.app_config_tool.tool.base.CommonUtil;
import jrmob.yonyou.com.commonlib.baseui.base.BaseActivityMob_lib;
import jrmob.yonyou.com.commonlib.login.bean.MAServices_lib;

/**
 * 描述: 会议室
 */
public class BoardroomActivity extends BaseActivityMob_lib {


    private ListView lv_huiyishi;
    private Button button_left;
    private List<MeetRoomVO> list;
    private String pk;
    private BoardroomAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initActionbar();
        setContentView(R.layout.schedulboardroomactivity);
        initView();
        initListener();
        initData();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if(v.getId() == R.id.button_left){
            finish();
        }
    }

    public void initView() {
        button_left = (Button) findViewById(R.id.button_left);
        lv_huiyishi = (ListView) findViewById(R.id.lv_huiyishi);

    }

    public void initListener() {
        button_left.setOnClickListener(this);
        lv_huiyishi.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                if (adapter.getItem(arg2).getIsusAble().equals("0")) {
                    Intent intent = new Intent(BoardroomActivity.this, ScheduleMainActivity.class);
                    intent.putExtra("roonNo", adapter.getItem(arg2).getRoonNo());
                    setResult(113, intent); // 113 代表响应吗
                    finish();
                } else {
                    Toast.makeText(BoardroomActivity.this, "该会议室已被使用", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    public void initData() {
        callService();
    }
    /***
     * 调用后台服务
     */
    private void callService() {

        JSONObject json = new JSONObject();

        try {
            json.put("transtype", "meetingroomlist");
            json.put("user_id", getUserInfo().getUserid());
            json.put("sessionkey", getUserInfo().getSessionkey());
            if (!CommonUtil.isNetworkConnected(this)) {
                CommonUtil.showToast(this, "当前网络不可用,请检查!");
                return;
            }
            MAServices_lib.callMACommonService(this, new ServiceCallback() {
                @Override
                public void execute(JSONObject json) {
                    JSONObject jsObject;
                    JSONArray jsArray;
                    try {
                        jsObject = (JSONObject) json.opt("result");
                        Object obj = jsObject.opt("retflag");
                        // 有错误的时候
                        if (null != obj && obj.toString().trim().equals("1")) {
                            String msg = jsObject.optString("msg").toString().trim();
                            showMessage(msg);
                            if(msg.contains("请重新登录")){
                                turnToLoginActivity(BoardroomActivity.this);
                            }
                            return;
                        }
                        jsArray = (JSONArray) jsObject.get("meetingroomlist");
                        list = new ArrayList<MeetRoomVO>();
                        for (int i = 0; i < jsArray.length(); i++) {
                            MeetRoomVO vo = new MeetRoomVO();
                            vo.setRoonNo(jsArray.getJSONObject(i).getString("roomno"));
                            vo.setRoomAddress(jsArray.getJSONObject(i).getString("roomaddress"));
                            vo.setIsusAble(jsArray.getJSONObject(i).getString("isusable"));
                            vo.setScheduled(jsArray.getJSONObject(i).getString("scheduled"));
                            list.add(vo);
                        }
                        adapter = new BoardroomAdapter(BoardroomActivity.this, list);
                        lv_huiyishi.setAdapter(adapter);


                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }
                }

                @Override
                public void error(String arg0, String arg1) {
                    // 设置登录按钮可用性(登录请求后台有反应后可用)
                    Toast.makeText(BoardroomActivity.this, arg0 + arg1,
                            Toast.LENGTH_LONG).show();
                }
            }, json);

        } catch (Exception e) {
            // 设置登录按钮可用性(登录请求后台有反应后可用)
            Log.e("mmmm", e + "");
            e.printStackTrace();
        }
    }

}
